﻿using ShopPowerTemplate.WcfService.ServiceReference;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PaypalService
{
    public sealed class PaypalPaymentService
    {
        static readonly PaypalPaymentService instance = new PaypalPaymentService();
        static PaypalPaymentService()
        {
        }
        PaypalPaymentService()
        {
        }
        public static PaypalPaymentService Instance
        {
            get
            {
                return instance;
            }
        }

        public string GetPaypalCheckOutUrl(decimal amount, int orderId)
        {
            var client = new PayPalAPIAAInterfaceClient();
            string certificateValue = ConfigurationManager.AppSettings["payPalCertificate"];
            string usernameValue = ConfigurationManager.AppSettings["username"];
            string passwordValue = ConfigurationManager.AppSettings["password"];
            string expressCheckoutValue = ConfigurationManager.AppSettings["expressCheckout"];
            //ConfigurationManager.AppSettings[""]
            string certPath = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, certificateValue);
            var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(certPath, "");
            client.ClientCredentials.ClientCertificate.Certificate = cert;

            var credentials = new CustomSecurityHeaderType
            {
                Credentials = new UserIdPasswordType
                {
                    Username = usernameValue,
                    Password = passwordValue,
                }
            };


            SetExpressCheckoutRequestDetailsType reqDetails = new SetExpressCheckoutRequestDetailsType();
            reqDetails.ReturnURL = "http://localhost:4748/RestService.svc/PaymentPending?orderId=" + orderId.ToString();
            reqDetails.CancelURL = "http://localhost:4748/RestService.svc/PaymentCanceled?orderId=" + orderId.ToString();
            reqDetails.NoShipping = "1";

            reqDetails.OrderTotal = new BasicAmountType();
            reqDetails.OrderTotal.currencyID = CurrencyCodeType.EUR;

            NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
            numberFormatInfo = (NumberFormatInfo)numberFormatInfo.Clone();
            numberFormatInfo.CurrencySymbol = "";
            numberFormatInfo.CurrencyDecimalSeparator = ".";
            numberFormatInfo.CurrencyGroupSeparator = ",";
            reqDetails.OrderTotal.Value = string.Format(numberFormatInfo, "{0:c}", amount);

            SetExpressCheckoutReq req = new SetExpressCheckoutReq()
            {
                SetExpressCheckoutRequest = new SetExpressCheckoutRequestType()
                {
                    Version = "60.0",
                    SetExpressCheckoutRequestDetails = reqDetails
                }
            };

            var res = client.SetExpressCheckout(ref credentials, req);

            if (res.Errors != null && res.Errors.Length > 0)
            {
                throw new Exception("Exception(s) occured when calling PayPal. First exception: " + res.Errors[0].LongMessage + res.Errors.Length.ToString());
            }

            return string.Format("{0}?cmd=_express-checkout&token={1}&useraction=commit", expressCheckoutValue, res.Token);
        }

    }

}
